package com.zqxq.lottery.service.analysis.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zqxq.lottery.common.constant.Constant;
import com.zqxq.lottery.common.utils.CommonUtil;
import com.zqxq.lottery.entity.po.analysis.AnalysisVotePO;
import com.zqxq.lottery.mapper.analysis.AnalysisVoteMapper;
import com.zqxq.lottery.service.analysis.IAnalysisForecastService;
import com.zqxq.lottery.service.analysis.IAnalysisVoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class AnalysisVoteServiceImpl extends ServiceImpl<AnalysisVoteMapper, AnalysisVotePO> implements IAnalysisVoteService {
    @Autowired
    private IAnalysisForecastService analysisForecastService;
    @Override
    public Map<String, Object> getVoteByUserId(String code, Long user_id) throws Exception {
        //官方支持率
        Map<String, Object> officialVote = analysisForecastService.getOfficialVote(code);
        Map<String, Object> result = new HashMap<>();
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("code", code);
        //总条数
        int count = baseMapper.selectCount(wrapper);
        Wrapper userWrapper = new EntityWrapper();
        userWrapper.eq("code", code);
        userWrapper.eq("user_id", user_id);
        //查询用户投票
        List<AnalysisVotePO> userList = baseMapper.selectList(userWrapper);
        //投票主胜次数
        Wrapper wrapper0 = new EntityWrapper();
        wrapper0.eq("code", code);
        wrapper0.eq("vote_type", Constant.VOTE_TYPE_0);
        int count0 = baseMapper.selectCount(wrapper0);
        //投票平次数
        Wrapper wrapper1 = new EntityWrapper();
        wrapper1.eq("code", code);
        wrapper1.eq("vote_type", Constant.VOTE_TYPE_1);
        int count1 = baseMapper.selectCount(wrapper1);
        if (officialVote != null){
            result.put("voteWin", parseOdd(officialVote.get("win_per").toString()));
            result.put("voteFlat", parseOdd(officialVote.get("draw_per").toString()));
            result.put("voteNegative", parseOdd(officialVote.get("lose_per").toString()));
        }else {
            if (count == 0) {
                result.put("voteWin", 0);
                result.put("voteFlat", 0);
                result.put("voteNegative", 0);
            } else {
                BigDecimal b0 = new BigDecimal(count0).divide(new BigDecimal(count), 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal b1 = new BigDecimal(count1).divide(new BigDecimal(count), 2, BigDecimal.ROUND_HALF_UP);
                //胜
                result.put("voteWin", b0);
                //平
                result.put("voteFlat", b1);
                //负
                result.put("voteNegative", new BigDecimal(1).subtract(b0).subtract(b1));
            }
        }

        if (CommonUtil.isNotEmpty(userList)) {
            result.put("vote", userList.get(0));
        } else {
            result.put("vote", null);
        }
        return result;
    }

    /**
    * @Author 苦智
    * @Description 百分数转小数
    * @Date 16:03 2018/9/30
    * @Param [per]  字符串类型的百分数
    * @return java.math.BigDecimal
    **/
    public static Number parseOdd(String per){
        NumberFormat nf=NumberFormat.getPercentInstance();
        Number m = 0;
        //NumberFormat是一个工厂，可以直接getXXX创建，而getPercentInstance() 是返回当前默认语言环境的百分比格式。
        try {
            m=nf.parse(per);//parse(xx)表示解析给定字符串开头的文本，生成一个数值，逐步地解析字符串的各部分
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return m;
    }
}
